<template>
  <div class="login-container">
    <div class="container">
      <div class="login-box">
        <h2>用户登录</h2>
        <input v-model="account" placeholder="账号" class="input" />
        <input v-model="password" type="password" placeholder="密码" class="input" />
        <div class="btn-row">
          <button class="login-btn" @click="handleLogin">登录</button>
          <button class="register-btn" @click="showRegister = true">注册</button>
        </div>
      </div>
      <RegisterDialog v-if="showRegister" @close="showRegister = false" @success="onRegisterSuccess" />
      <div v-if="showMsg" class="custom-alert-mask">
        <div class="custom-alert-box">
          <div class="custom-alert-content">{{ msgContent }}</div>
          <button class="custom-alert-btn" @click="showMsg = false">知道了</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import axios from '../axios';
import RegisterDialog from '../components/RegisterDialog.vue';

const router = useRouter();
const account = ref('');
const password = ref('');
const showRegister = ref(false);
const showMsg = ref(false);
const msgContent = ref('');

const handleLogin = async () => {
  if (!account.value || !password.value) {
    showAlert('请输入账号和密码');
    return;
  }
  try {
    const res = await axios.get('/user/doLogin', {
      params: {
        account: account.value,
        password: password.value
      }
    });
    if (res.data.code === 200) {
      localStorage.setItem('satoken', res.data.data.tokenValue || '');
      router.replace('/home');
    } else {
      showAlert('登录失败: ' + (res.data?.msg || '未知错误'));
    }
  } catch (e) {
    showAlert('登录接口异常');
  }
};

const onRegisterSuccess = ({ account: regAccount, password: regPassword }) => {
  account.value = regAccount;
  password.value = regPassword;
};

function showAlert(msg) {
  msgContent.value = msg;
  showMsg.value = true;
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f7f8fa;
}
.login-box {
  background: #fff;
  border-radius: 10px;
  box-shadow: 0 4px 24px #00000010;
  padding: 36px 28px 28px 28px;
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  max-width: 360px;
  margin: 0 auto;
}
.login-box h2 {
  font-size: 24px;
  font-weight: 600;
  margin-bottom: 28px;
  color: #222;
  letter-spacing: 1px;
}
.input {
  width: 100%;
  margin: 14px 0 0 0;
  padding: 12px 14px;
  border: 1px solid #e0e3e8;
  border-radius: 6px;
  font-size: 16px;
  outline: none;
  background: #f9fafb;
  transition: border 0.2s;
}
.input:focus {
  border: 1.5px solid #409eff;
  background: #fff;
}
.btn-row {
  display: flex;
  gap: 16px;
  width: 100%;
  margin-top: 24px;
  justify-content: space-between;
}
.login-btn {
  flex: 1;
  background: linear-gradient(90deg, #409eff 0%, #66b1ff 100%);
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 14px 0;
  font-size: 18px;
  font-weight: 700;
  letter-spacing: 1px;
  box-shadow: 0 4px 16px #409eff33, 0 1.5px 0 #fff inset;
  transition: background 0.2s, box-shadow 0.2s, transform 0.1s;
  position: relative;
  overflow: hidden;
  margin-right: 8px;
  cursor: pointer;
}
.login-btn:hover {
  background: linear-gradient(90deg, #3076c9 0%, #409eff 100%);
  box-shadow: 0 8px 24px #409eff44, 0 1.5px 0 #fff inset;
  transform: translateY(-2px) scale(1.03);
}
.register-btn {
  flex: 1;
  background: linear-gradient(90deg, #67c23a 0%, #b3e19d 100%);
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 14px 0;
  font-size: 18px;
  font-weight: 700;
  letter-spacing: 1px;
  box-shadow: 0 4px 16px #67c23a33, 0 1.5px 0 #fff inset;
  transition: background 0.2s, box-shadow 0.2s, transform 0.1s;
  position: relative;
  overflow: hidden;
  margin-left: 8px;
  cursor: pointer;
}
.register-btn:hover {
  background: linear-gradient(90deg, #409eff 0%, #67c23a 100%);
  box-shadow: 0 8px 24px #67c23a44, 0 1.5px 0 #fff inset;
  transform: translateY(-2px) scale(1.03);
}
.custom-alert-mask {
  position: fixed;
  left: 0; top: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.18);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2000;
}
.custom-alert-box {
  background: #f7faff;
  border: 2px solid #409eff;
  border-radius: 14px;
  padding: 28px 32px 20px 32px;
  min-width: 220px;
  box-shadow: 0 4px 24px #409eff22;
  text-align: center;
}
.custom-alert-content {
  color: #3076c9;
  font-size: 17px;
  margin-bottom: 18px;
  font-weight: bold;
  letter-spacing: 1px;
}
.custom-alert-btn {
  background: linear-gradient(90deg, #409eff 0%, #66b1ff 100%);
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 8px 32px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
  box-shadow: 0 2px 8px #409eff33;
}
.custom-alert-btn:hover {
  background: linear-gradient(90deg, #3076c9 0%, #409eff 100%);
}
</style>
